package integration

import (
	"context"
	compb "gitee.com/micro-services/protocol/common"
	pb "gitee.com/micro-services/protocol/room/api"
	mpb "gitee.com/micro-services/protocol/room/server"
)

func CreateRoomDevice(ctx context.Context, r *pb.APICreateRoomDeviceReq) (*pb.APICreateRoomDeviceRsp, error) {
	rsp, err := GetProxyManger().RoomDeviceClientProxy.CreateRoomDevice(ctx, &mpb.CreateRoomDeviceReq{
		Info: &compb.RoomDeviceInfo{
			DeviceID:      r.Info.DeviceID,
			RoomID:        r.Info.RoomID,
			RoomNo:        r.Info.RoomNo,
			BedNum:        r.Info.BedNum,
			BedImage:      r.Info.BedImage,
			BedAttribute:  r.Info.BedAttribute,
			PeopleNum:     r.Info.PeopleNum,
			HasNetWork:    r.Info.HasNetWork,
			HasWifi:       r.Info.HasWifi,
			HasTV:         r.Info.HasTV,
			HasTel:        r.Info.HasTel,
			HasBreakfast:  r.Info.HasBreakfast,
			NoticeID:      r.Info.NoticeID,
			NoticeContent: r.Info.NoticeContent,
		},
	})
	if err != nil {
		return nil, err
	}

	return &pb.APICreateRoomDeviceRsp{
		RetCode: rsp.RetCode,
		Msg:     rsp.Msg,
	}, nil
}

func UpdateRoomDeviceInfo(ctx context.Context, r *pb.APIUpdateRoomDeviceInfoReq) (*pb.APIUpdateRoomInfoRsp, error) {
	rsp, err := GetProxyManger().RoomDeviceClientProxy.UpdateRoomDeviceInfo(ctx, &mpb.UpdateRoomDeviceInfoReq{
		DeviceID:     r.DeviceID,
		BedNum:       r.BedNum,
		BedImage:     r.BedImage,
		BedAttribute: r.BedAttribute,
		PeopleNum:    r.PeopleNum,
		HasNetWork:   r.HasNetWork,
		HasWifi:      r.HasWifi,
		HasTV:        r.HasTV,
		HasTel:       r.HasTel,
		HasBreakfast: r.HasBreakfast,
		NoticeID:     r.NoticeID,
	})
	if err != nil {
		return nil, err
	}

	return &pb.APIUpdateRoomInfoRsp{
		RetCode: rsp.RetCode,
		Msg:     rsp.Msg,
	}, nil
}

func DeleteRoomDevice(ctx context.Context, r *pb.APIDeleteRoomDeviceReq) (*pb.APIDeleteRoomDeviceRsp, error) {
	rsp, err := GetProxyManger().RoomDeviceClientProxy.DeleteRoomDevice(ctx, &mpb.DeleteRoomDeviceReq{
		DeviceID: r.DeviceID,
	})
	if err != nil {
		return nil, err
	}

	return &pb.APIDeleteRoomDeviceRsp{
		RetCode: rsp.RetCode,
		Msg:     rsp.Msg,
	}, nil
}

func SearchRoomDevice(ctx context.Context, r *pb.APISearchRoomDeviceReq) (*pb.APISearchRoomDeviceRsp, error) {
	rsp, err := GetProxyManger().RoomDeviceClientProxy.SearchRoomDevice(ctx, &mpb.SearchRoomDeviceReq{
		RoomNo:    r.RoomNo,
		BedNum:    r.BedNum,
		PeopleNum: r.PeopleNum,
		Offset:    r.Offset,
		Limit:     r.Limit,
	})
	if err != nil {
		return nil, err
	}

	return &pb.APISearchRoomDeviceRsp{
		List:  rsp.List,
		Count: rsp.Count,
	}, nil
}

func DeviceList(ctx context.Context, r *pb.APIDeviceListReq) (*pb.APIDeviceListRsp, error) {
	rsp, err := GetProxyManger().RoomDeviceClientProxy.DeviceList(ctx, &mpb.DeviceListReq{
		RoomID: r.RoomID,
	})
	if err != nil {
		return nil, err
	}

	list := make([]*pb.DeviceList, 0)
	if rsp.List != nil {
		for _, v := range rsp.List {
			list = append(list, &pb.DeviceList{
				DeviceID: v.DeviceID,
				RoomNo:   v.RoomNo,
			})
		}
	}

	return &pb.APIDeviceListRsp{
		List: list,
	}, nil
}
